package com.seaboxdata.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.ftp.FTPFileSystem;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.net.URI;

/**
 * @author
 * @create 2021-02-05 10:10
 **/
public class HdfsTest {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        String krb5Path = "F:/dfjx/keytab/krb5.conf";
        String keyTabPath = "F:/dfjx/keytab/hdfs.headless.keytab";
        System.setProperty("java.security.krb5.conf", "F:/dfjx/keytab/krb5.conf");
        System.setProperty("sun.security.krb5.debug", "false");
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        conf.set("keytab.file", "F:/dfjx/keytab/hdfs.headless.keytab");
        conf.set("kerberos.principal", "hdfs-dpzw@HADOOP.COM");
        try {
            UserGroupInformation.loginUserFromKeytab("hdfs-dpzw@HADOOP.COM", keyTabPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileSystem fs = null;
        FTPFileSystem fsLocal = new FTPFileSystem();
        Configuration localConf = new Configuration();
        localConf.set("fs.ftp.host","203.91.47.158");
        localConf.set("fs.ftp.host.port","22");
        localConf.set("fs.ftp.password.203.91.47.158","dpxq.2017");
        localConf.set("fs.ftp.user.203.91.47.158","szgov");




        FSDataInputStream in = null;
        try {
            fsLocal.initialize(URI.create("ftp//203.91.47.158:22"),localConf);
            FileSystem[] childFileSystems = fsLocal.getChildFileSystems();
            for (FileSystem childFileSystem : childFileSystems) {
                //childFileSystem.
            }
            RemoteIterator<LocatedFileStatus> files = fsLocal.listFiles(new Path("/home/szgov/"), true);
            while(files.hasNext()){
                files.next();
            }
            /*fsLocal.get
            FileStatus fileStatus = fsLocal.getFileStatus(new Path("/home/szgov"));*/
            in = fsLocal.open(new Path("/home/szgov/"));
            //new File(in);
            fs = FileSystem.get(URI.create("hdfs://172.21.17.46:8020"), conf);
            FSDataOutputStream out = fs.create(new Path("/tmp.txt"));
            IOUtils.copyBytes(in,out,4096,true);

        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
